package cn.hnbjx.prepaid.service;

import cn.hnbjx.prepaid.dao.AccountDao;
import cn.hnbjx.prepaid.dao.RescueUserDao;
import cn.hnbjx.prepaid.dao.UserHelpDao;
import cn.hnbjx.prepaid.pojo.Account;
import cn.hnbjx.prepaid.pojo.NumData;
import cn.hnbjx.prepaid.pojo.RescueUser;
import cn.hnbjx.prepaid.pojo.UserHelp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserHelpService {
    @Autowired
    private UserHelpDao userHelpDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private RescueUserDao rescueUserDao;

    public PageInfo<UserHelp> findAll(int page, String key) {
        PageHelper.startPage(page,10);
        List<UserHelp> userHelps = userHelpDao.findAll(key);
        for (UserHelp userHelp : userHelps) {
            Account account = accountDao.findById(userHelp.getAccountId());
            userHelp.setAccount(account);
        }
        PageInfo<UserHelp> userHelpsPageInfo = new PageInfo<>(userHelps);
        return userHelpsPageInfo;
    }
    public PageInfo<UserHelp> apifindAll(int page, int type,int id) {
        PageHelper.startPage(page,10);
        List<UserHelp> userHelps = userHelpDao.apifindAll(type,id);
        for (UserHelp userHelp : userHelps) {
            Account account = accountDao.findById(userHelp.getAccountId());
            userHelp.setAccount(account);
            if(userHelp.getFlag()>1){
             RescueUser rescueUser= rescueUserDao.findByHelpId(userHelp.getId());
                Account account1 = accountDao.findById(rescueUser.getRescueUserId());
                userHelp.setRescueUser(account1);
            }
        }
        PageInfo<UserHelp> userHelpsPageInfo = new PageInfo<>(userHelps);
        return userHelpsPageInfo;
    }
    public void insert(UserHelp userHelp){
        userHelpDao.insert(userHelp);
        return;
    }
    public int updateFlag(int flag,int id){
        return userHelpDao.updateFlag(flag,id);
    }
    public  List<UserHelp> findByProvince(String key){
        List<UserHelp> userHelps= userHelpDao.findByProvince(key);
        for (UserHelp userHelp : userHelps) {
            Account account = accountDao.findById(userHelp.getAccountId());
            userHelp.setAccount(account);
            if(userHelp.getFlag()>1){
                RescueUser rescueUser= rescueUserDao.findByHelpId(userHelp.getId());
                Account account1 = accountDao.findById(rescueUser.getRescueUserId());
                userHelp.setRescueUser(account1);
            }
        }
       return userHelps;
    }
    public  List<NumData> findByNum(){
        return  userHelpDao.findByNum();
    }
}
